<template>
	<view>
		<button @getuserinfo="getUserInfo" open-type="getUserInfo" type="primary">微信登录并授权</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {
			getUserInfo(data) {
				//通过微信登录接口获取用户id

				uni.login({
					success: (res) => {
						//code 调用微信接口换取用户id
						const wxloginAPI =
							'https://api.weixin.qq.com/sns/jscode2session?appid=wx3379382a1f01164a&secret=70b95274dd4f9eba48f1d3e10d648c98&js_code=' +
							res.code + '&grant_type=authorization_code'
						//GET方式请求接口
						uni.request({
							url: wxloginAPI,
							method: 'GET',
							data: {},
							success: res => {
								this.$store.commit('login', {
									id: res.data.openid,
									username: data.detail.userInfo.nickName,
									avatar: data.detail.userInfo.avatarUrl
								})
								//跳转到我的页面
								uni.switchTab({
									url:'/pages/me/me'
								})
							},
							fail: () => {},
							complete: () => {}
						});
					}
				})
			}

		}
	}
</script>

<style>
	button {
		width: 60%;
		margin-top: 100rpx;
	}
</style>
